using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Text;


namespace WindowsGame.ScoreComponents
{
    /// <summary>
    /// This component will take care of drawing and calculating time
    /// </summary>
    public class TimerComponent : Microsoft.Xna.Framework.GameComponent
    {
        //Default font
        SpriteFont hudFont;

        //Use built in type "time span" to calculate time. This will be handy since it contains ways to easily access seconds, minutes etc
        TimeSpan timer;
        String timerString; //String for drawing out time
        StringBuilder sb; //Create string builder for creating string

        //Bool for determining whether to calculate time or not. Usefull in example cutscenes
        bool pauseTimer;

        Vector2 timerLocation;

        //HUD
        Texture2D hudSprite;
        Rectangle hudRectangle; //Rectangle for drawing HUD

        int screenWidth, screenHeight;

        public TimerComponent(Game game, int screenWidth, int screenHeight)
            : base(game)
        {
            this.screenWidth = screenWidth;
            this.screenHeight = screenHeight;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public void Initialize()
        {
            //Set initial variables
            timerLocation = new Vector2(screenWidth - 124, screenHeight - 19);
            pauseTimer = false;
            sb = new StringBuilder();
            timerString = "";

            base.Initialize();
        }

        public void LoadContent(ContentManager Content)
        {
            //Load font
            hudFont = Content.Load<SpriteFont>("Fonts\\PressStart2P_12");

            //Load HUD graphic
            hudSprite = Content.Load<Texture2D>("GFX\\HUD\\TimeBarHUD");
            //Create rectangle after loading sprite to get right dimensions of sprite
            hudRectangle = new Rectangle(screenWidth - hudSprite.Width, screenHeight - hudSprite.Height, hudSprite.Width, hudSprite.Height);
        }


        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            //Update timer
            if (!pauseTimer)
            {
                //Add gamtimes time to make game FPS-independent
                //(Gametime will return how much time it took to draw last frame)
                timer += gameTime.ElapsedGameTime;

                //Construct string. We will be using string buildier
                sb.Clear();


                if (timer.Hours < 10)
                {
                    sb.Append("0");
                    sb.Append(timer.Hours.ToString());
                    sb.Append(":");
                }
                else
                {
                    sb.Append(timer.Hours.ToString());
                    sb.Append(":");
                }

                if (timer.Minutes < 10)
                {
                    sb.Append("0");
                    sb.Append(timer.Minutes.ToString());
                    sb.Append(":");
                }
                else
                {
                    sb.Append(timer.Minutes.ToString());
                    sb.Append(":");
                }
                if (timer.Seconds < 10)
                {
                    sb.Append("0");
                    sb.Append(timer.Seconds.ToString());
                }
                else
                    sb.Append(timer.Seconds.ToString());

                timerString = sb.ToString();
            }

            base.Update(gameTime);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            //Draw timer
            spriteBatch.Begin();

            spriteBatch.Draw(hudSprite, hudRectangle, Color.White);

            spriteBatch.DrawString(hudFont, timerString, timerLocation, Color.White);

            spriteBatch.End();
        }

        //Getter for getting time
        public TimeSpan GetTime() { return this.timer; }

        //Method for resetting timer
        public void ResetTimer() { timer = TimeSpan.FromMilliseconds(0); }

        //Pause and unpause timer
        public void PauseTimer() { pauseTimer = true; }
        public void ResumePauseTimer() { pauseTimer = false; }

        //Getter for checking if timer is paused or not
        public bool IsPaused() { return this.pauseTimer; }
    }
}
